﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using IAOI.Platform.Common.Configuration;
using IAOI.Platform.Common.Diagnostics;

namespace IAOI.Platform.WCF.WCF
{
    public static class DataBaseLogging //: ILogging
    {
        public static void WriteLogging(ServiceLogData loggingData)
        {
            try
            {
                //Action<object> action = (object obj) =>
                //{
                //    Console.WriteLine("Task={0}, obj={1}, Thread={2}", Task.CurrentId, obj.ToString(), Thread.CurrentThread.ManagedThreadId);
                //};


                //System.Threading.Tasks.Task t2 = Task.Factory.StartNew(action, "beta");

                //System.Threading.ThreadPool.QueueUserWorkItem(delegate
                //    {
                //        try
                //        {

                //            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SOADB"].ConnectionString;
                //            using (SqlConnection con = new SqlConnection(connectionString))
                //            using (SqlCommand sc = new SqlCommand("dbo.up_RealTimeLog_Save", con))
                //            {
                //                sc.CommandType = System.Data.CommandType.StoredProcedure;
                //                sc.Parameters.AddWithValue("@ContextID", loggingData.ContextID);
                //                sc.Parameters.AddWithValue("@StartTime", loggingData.StartTime);
                //                sc.Parameters.AddWithValue("@EndTime", loggingData.EndTime);

                //                if (loggingData.EndTime != DateTimeOffset.MinValue && loggingData.StartTime != DateTimeOffset.MinValue)
                //                    sc.Parameters.AddWithValue("@Duration", Convert.ToInt32(loggingData.EndTime.Subtract(loggingData.StartTime).TotalMilliseconds));
                //                else
                //                    sc.Parameters.AddWithValue("@Duration", 0);

                //                sc.Parameters.AddWithValue("@State", Convert.ToInt16(loggingData.State));
                //                sc.Parameters.AddWithValue("@Message", loggingData.Message);
                //                sc.Parameters.AddWithValue("@HostName", loggingData.HostName);
                //                sc.Parameters.AddWithValue("@Action", loggingData.Action);
                //                sc.Parameters.AddWithValue("@AbsoluteUri", loggingData.AbsoluteUri);
                //                sc.Parameters.AddWithValue("@InputMessage", loggingData.InputMessage);
                //                sc.Parameters.AddWithValue("@OutputMessage", loggingData.OutputMessage);
                //                sc.Parameters.AddWithValue("@CallerID", loggingData.CallerID);
                //                sc.Parameters.AddWithValue("@CallerName", loggingData.CallerName);
                //                sc.Parameters.AddWithValue("@CallerIP", loggingData.CallerIP);

                //                foreach (SqlParameter parameter in sc.Parameters)
                //                {
                //                    if (parameter.Value == null)
                //                    {
                //                        parameter.Value = DBNull.Value;
                //                    }
                //                }
                //                con.Open();
                //                sc.ExecuteNonQuery();
                //            }
                //        }
                //        catch (Exception ex)
                //        {
                //            EventLog.WriteEntry(EventLogSource.Platform, ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
                //        }
                //    }
                //);
            }
            catch (Exception ex)
            {
                //EventLog.WriteEntry(EventLogSource.Platform, ex.Message + ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }
        }


    }
}
